home *** CD-ROM | disk | FTP | other *** search
- /* FontMenu.c
- * Handle font and style menus in Writeswell Jr.
- * ©1992 Working Software, Inc.
- * This source code is copyrighted. Permission is granted to use the Word Services
- * portion of the Writeswell Jr. source code in your own programs, but you
- * may not distribute the Writeswell Jr. word-processor code as a
- * commercial product. If you modify the code, please do not call it
- * Writeswell Jr. (or Writeswell.) This will ensure that people understand the
- * program and don’t have to deal with a number of different versions with
- * who-knows-what going on in the code.
- *
- * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
- *
- * 19 May 92 Mike Crawford
- */
-
- #include <EPPC.h>
- #include <AppleEvents.h>
- #include <AEObjects.h>
- #include <Script.h>
- #include "AERegistry.h"
- #include "WordServices.h"
- #include "TestBed.h"
- #include "TBConstants.h"
- #include "Gripe.h"
- #include "AppEvents.h"
- #include "TBGlobals.h"
- #include "FontMenu.h"
- #include "OutlineButton.h"
-
- void FixPasteItem( MenuHandle editMenu );
-
- static Style gStyleTable[] = { 0, bold, italic, underline, outline, shadow, condense, extend };
- static short gPointSizeTable[] = { 9, 10, 12, 14, 18, 24, 48, 72 };
-
- Boolean DoFontMenu( short theItem )
- {
- TEHandle textH;
- TextStyle styleRec;
- MenuHandle fontMenu;
- short fontNum;
- Str255 fontName;
-
- if ( !gDocWindow )
- return true;
-
- if ( gReadOnly )
- return true;
-
- textH = (TEHandle)GetWRefCon( gDocWindow );
-
- fontMenu = GetMHandle( kFontMenuID );
- if ( !fontMenu )
- return false;
-
- GetItem( fontMenu, theItem, fontName );
-
- GetFNum( fontName, &fontNum );
-
- /* Set TE to use the new font */
-
- styleRec.tsFont = fontNum;
-
- TESetStyle( doFont, &styleRec, true, textH );
-
- gDocDirty = true;
-
- return true;
- }
-
- Boolean DoStyleMenu( short theItem )
- {
- TEHandle textH;
-
- if ( theItem == kStMOtherPoint ){
- CustomSize();
- return true;
- }
-
- /* 2.0 MDC handle read-only documents */
-
- if ( gReadOnly )
- return true;
-
- gDocDirty = true;
-
- if ( theItem < kStMDash ){
- SetStyle( theItem );
- return true;
- }
-
- SetSize( theItem );
-
- return true;
- }
-
- void CustomSize( void )
- {
- TextStyle styleRec;
- short fontAscent;
- short lineHeight;
- TEHandle textH;
- DialogPtr custDlg;
- short item;
- Str255 textStr;
- short kind;
- Handle h;
- Rect r;
- long num;
- UserItemUPP outlineUPP;
-
- if ( !gDocWindow )
- return;
-
- textH = (TEHandle)GetWRefCon( gDocWindow );
-
- custDlg = GetNewDialog( kCustomSizeID, (Ptr)NULL, (WindowPtr)-1 );
- if ( !custDlg )
- return;
-
- /* Get the current size */
-
- TEGetStyle( (*textH)->selStart, &styleRec, &lineHeight, &fontAscent, textH );
- NumToString( (long)fontAscent, textStr );
-
- GetDItem( custDlg, kCSEntryField, &kind, &h, &r );
- SetIText( h, textStr );
-
- TESetSelect( 0L, 32000L, ((DialogPeek)custDlg)->textH );
-
- /* Set up a user proc to draw the default outline */
-
- GetDItem( custDlg, kCSDefUser, &kind, &h, &r );
-
- outlineUPP = NewUserItemProc( OutlineButton );
- if ( !outlineUPP )
- return; // STUB should report an error
-
- SetDItem( custDlg, kCSDefUser, kind, (Handle)outlineUPP, &r );
-
- do {
- ModalDialog( (ModalFilterUPP)NULL, &item );
- } while ( item != kCSCancel && item != kCSOK );
-
- if ( item == kCSCancel ){
- DisposDialog( custDlg );
- return;
- }
-
- GetDItem( custDlg, kCSEntryField, &kind, &h, &r );
- GetIText( h, textStr );
-
- #ifdef GENERATINGCFM
- DisposeRoutineDescriptor( outlineUPP );
- #endif
-
- DisposDialog( custDlg );
-
- StringToNum( textStr, &num );
-
- /* Set TE to use the new style */
-
- styleRec.tsSize = (short)num;
-
- TESetStyle( doSize, &styleRec, true, textH );
-
- gDocDirty = true; /* Note that it's not dirty if we canceled */
-
- return;
- }
-
- void SetStyle( short theItem )
- {
- TextStyle styleRec;
- TEHandle textH;
-
- if ( !gDocWindow )
- return;
-
- textH = (TEHandle)GetWRefCon( gDocWindow );
-
- /* Set TE to use the new style */
-
- styleRec.tsFace = gStyleTable[ theItem - 1 ];
-
- TESetStyle( doFace, &styleRec, true, textH );
-
- return;
- }
-
- void SetSize( short theItem )
- {
- TextStyle styleRec;
- TEHandle textH;
-
- if ( !gDocWindow )
- return;
-
- textH = (TEHandle)GetWRefCon( gDocWindow );
-
- theItem -= kStM9Point;
-
- styleRec.tsSize = gPointSizeTable[ theItem ];
-
- TESetStyle( doSize, &styleRec, true, textH );
-
- return;
- }
-
- void InitStyleMenu( void )
- {
- MenuHandle styleMenu;
- short i;
-
- styleMenu = (MenuHandle)GetResource( 'MENU', kStyleMenuID );
- if ( !styleMenu ){
- Gripe( "\pcannot get Style Menu Handle" );
- return;
- }
-
- for ( i = kStMBold; i < kStMDash; i++ ){
- SetItemStyle( styleMenu, i, gStyleTable[ i - 1 ] );
- }
-
- return;
- }
-
- void FixMenuMarks( void )
- {
- TextStyle styleRec;
- TEHandle textH;
- MenuHandle fileMenu;
- MenuHandle fontMenu;
- MenuHandle styleMenu;
- MenuHandle editMenu;
- short numItems;
- short i;
- short fontNum;
- short itemSize;
- short lineHeight;
- short fontAscent;
- Str255 fontName;
- Str255 itemName;
- short mode;
- Boolean continuous;
-
- fileMenu = (MenuHandle)GetResource( 'MENU', kFileMenuID );
- if ( !fileMenu )
- return;
-
- editMenu = (MenuHandle)GetResource( 'MENU', kEditMenuID );
- if ( !editMenu )
- return;
-
- fontMenu = (MenuHandle)GetResource( 'MENU', kFontMenuID );
- if ( !fontMenu )
- return;
-
- styleMenu = (MenuHandle)GetResource( 'MENU', kStyleMenuID );
- if ( !styleMenu )
- return;
-
- DisableItem( editMenu, kEMUndo );
-
- if ( !gDocWindow ){
-
- EnableItem( fileMenu, kFMNew );
- EnableItem( fileMenu, kFMOpen );
- DisableItem( fileMenu, kFMClose );
- DisableItem( fileMenu, kFMSave );
- DisableItem( fileMenu, kFMSaveAs );
- DisableItem( fileMenu, kFMPageSetup );
- DisableItem( fileMenu, kFMPrint );
-
- DisableItem( editMenu, kEMPaste );
- #ifdef NEVER
- /* STUB Do this differently. Don't want to redraw menu bar while mouse is down */
- DisableItem( fontMenu, 0 ); /* Disable the entire font menu */
- DisableItem( styleMenu, 0 ); /* Disable the entire style menu */
- DrawMenuBar();
- #endif
- return;
- }
-
- SetPort( gDocWindow );
-
- DisableItem( fileMenu, kFMNew );
- DisableItem( fileMenu, kFMOpen );
- EnableItem( fileMenu, kFMClose );
- EnableItem( fileMenu, kFMSave );
- EnableItem( fileMenu, kFMSaveAs );
- EnableItem( fileMenu, kFMPageSetup );
- EnableItem( fileMenu, kFMPrint );
-
- FixPasteItem( editMenu );
-
- #ifdef NEVER
- /* STUB Do this differently. Don't want to redraw menu bar while mouse is down */
- EnableItem( fontMenu, 0 ); /* Enable the entire font menu */
- EnableItem( styleMenu, 0 ); /* Enable the entire style menu */
- #endif
-
- textH = (TEHandle)GetWRefCon( gDocWindow );
-
- /* Get the current font */
-
- #ifdef NEVER
- TEGetStyle( (*textH)->selStart, &styleRec, &lineHeight, &fontAscent, textH );
- #endif
-
- styleRec.tsFont = 0;
- styleRec.tsSize = 0;
- styleRec.tsFace = 0;
-
- mode = doFont + doFace + doSize;
-
- /* BUG BUG BUG TEContinousStyle does not appear to work as advertised */
-
- continuous = TEContinuousStyle( &mode, &styleRec, textH );
-
- if ( mode & doFont ){
- fontNum = styleRec.tsFont;
-
- if ( fontNum == applFont )
- fontNum = 3 /*GetAppFont()*/; /* Otherwise Geneva shows up as 1 */
-
- GetFontName( fontNum, fontName );
- }else{
- fontName[ 0 ] = '\0';
- }
-
- numItems = CountMItems( fontMenu );
-
- for ( i = 1; i <= numItems; i++ ){
- GetItem( fontMenu, i, itemName );
-
- if ( PStrCmp( itemName, fontName ) ){
- CheckItem( fontMenu, i, true );
- } else {
- CheckItem( fontMenu, i, false );
- }
- }
-
- /* Show the real font sizes */
-
- for ( i = 0; i < kStMDash2 - kStM9Point; i++ ){
-
- itemSize = gPointSizeTable[ i ];
-
- if ( RealFont( fontNum, itemSize ) ){
- SetItemStyle( styleMenu, i + kStM9Point, outline );
- }else{
- SetItemStyle( styleMenu, i + kStM9Point, 0 );
- }
-
- if ( mode & doSize && itemSize == styleRec.tsSize ){ /* 1.0b2 MDC was fontAscent */
- CheckItem( styleMenu, i + kStM9Point, true );
- } else {
- CheckItem( styleMenu, i + kStM9Point, false );
- }
- }
-
- /* If the styles are the same, check them all, otherwise check none */
-
- for ( i = kStMPlain; i <= kStMExtend; i++ ){
-
- if ( mode & doFace ){
-
- if ( gStyleTable[ i - 1 ] & styleRec.tsFace )
- CheckItem( styleMenu, i, true );
- }else{
- CheckItem( styleMenu, i, false );
- }
- }
-
- return;
- }
-
- void FixPasteItem( MenuHandle editMenu )
- {
- PScrapStuff scrapPtr;
- long memFree;
-
- /* It is possible to cause a crash if an application
- * has put a lot on the scrap, even if it is not text.
- * This can happen with Microsoft Word when it puts a
- * huge RTF on the scrap - we crash when we try to paste in
- * the text. I think this may actually be a bug in System 7.
- *
- * To prevent this, we disable the paste menu item if
- * there is not enough free memory to hold the whole
- * scrap. This is not a rigorous test, since FreeMem
- * does not tell how big a single block can be, but it
- * is reasonably fast.
- */
-
-
- memFree = FreeMem();
-
- scrapPtr = InfoScrap();
-
- if ( !scrapPtr || scrapPtr->scrapSize == 0 || scrapPtr->scrapSize > memFree ){
- DisableItem( editMenu, kEMPaste );
- return;
- }
-
- EnableItem( editMenu, kEMPaste );
-
- return;
- }